<template>
  <view class="normal-login-container">

    <view class="item-wrap" style="margin-bottom: 0">
      <view class="lineBottom10">
        <uni-section title="商业险信息" type="line"/>
      </view>

      <uni-forms ref="ruleForm" :rules="rules" :label-position="boole ? 'top' : 'left'" :model="ruleForm"
                 labelWidth="auto">

        <uni-forms-item label="禀议内供应商:">
          <uni-easyinput v-if="boole" v-model="ruleForm.rentInsuranceCompany" disabled
                         placeholder="请输入禀议内供应商"/>
          <text v-else> {{ ruleForm.rentInsuranceCompany || '-' }}</text>
        </uni-forms-item>

        <uni-forms-item label="投保渠道:">
          <zxz-uni-data-select v-if="boole" v-model="ruleForm.insuranceChannels.toString()"
                               :localdata="dict.type.insurance_channels" dataKey="label" dataValue="value"
                               placeholder="请选择责任类型"
                               disabled :clear="true" filterable/>
          <text v-else> {{ handleLabel(insuranceList, ruleForm.insuranceChannels) }}
          </text>
        </uni-forms-item>

        <uni-forms-item label="被保险人:" name="syxInsuredName" :required="boole"
                        :rules="[{required:true, errorMessage: '请输入被保险人',validateTrigger:'blur'}]">
          <text class="syxInsuredName"/>
          <uni-easyinput v-if="boole" v-model="ruleForm.syxInsuredName" placeholder="请输入被保险人"/>
          <text v-else> {{ ruleForm.syxInsuredName || '-' }}</text>
        </uni-forms-item>

        <uni-forms-item label="商业险公司:" name="syxCompanyId" :required="boole"
                        :rules="[{required:true, errorMessage: '请选择商业险公司',validateTrigger:'bind'}]">
          <text class="syxCompanyId"/>
          <zxz-uni-data-select v-if="boole" v-model="ruleForm.syxCompanyId" :localdata="options"
                               dataKey="supplierName" dataValue="supplierId"
                               placeholder="请选择商业险公司"
                               @change="handleSyx"
                               :clear="true" filterable/>
          <text v-else>{{ ruleForm.syxCompany || '-' }}</text>
        </uni-forms-item>

        <uni-forms-item label="商业险公司编码:" name="syxCompanyCode" :required="boole"
                        :rules="[{required:true, errorMessage: '请输入商业险公司编码',validateTrigger:'blur'}]">
          <text class="syxCompanyCode"/>
          <uni-easyinput v-if="boole" disabled v-model="ruleForm.syxCompanyCode" placeholder="请输入商业险公司编码"/>
          <text v-else> {{ ruleForm.syxCompanyCode || '-' }}</text>
        </uni-forms-item>

        <uni-forms-item label="商业险起保日:" name="syxExpectBeginTime" :required="boole"
                        :rules="[{required:true, errorMessage: '请选择商业险起保日',validateTrigger:'bind'}]">
          <text class="syxExpectBeginTime"/>
          <uni-datetime-picker v-if="boole" type="date" placeholder="请选择商业险起保日"
                               @change="handleTime"
                               return-type="string" v-model="ruleForm.syxExpectBeginTime"/>
          <text v-else> {{ ruleForm.syxExpectBeginTime || '-' }}</text>
        </uni-forms-item>

        <uni-forms-item label="商业险到期日:" name="syxExpectEndTime" :required="boole"
                        :rules="[{required:true, errorMessage: '请选择商业险到期日',validateTrigger:'bind'}]">
          <text class="syxExpectEndTime"/>
          <uni-datetime-picker v-if="boole" type="date" placeholder="请选择商业险到期日"
                               :start="handleEffectDate(ruleForm.syxExpectBeginTime)"
                               return-type="string" v-model="ruleForm.syxExpectEndTime"/>
          <text v-else> {{ ruleForm.syxExpectEndTime || '-' }}</text>
        </uni-forms-item>

        <uni-forms-item label="商业险性质:" name="syxInsuranceType" :required="boole"
                        :rules="[{required:true, errorMessage: '请选择商业险性质',validateTrigger:'bind'}]">
          <text class="syxInsuranceType"/>
          <uni-data-select v-if="boole" v-model="ruleForm.syxInsuranceType" :localdata="range"
                           @change="handleInsuranceType"
                           placeholder="请选择商业险性质" :clear="true" filterable/>
          <text v-else> {{
              ruleForm.syxInsuranceType == '1' ? '运营' : ruleForm.syxInsuranceType == '2' ? '非运营' : ''
            }}
          </text>
        </uni-forms-item>

        <uni-forms-item label="商业险保单号:" name="syxPolicyCode" :required="boole"
                        :rules="[{required:true, errorMessage: '请输入商业险保单号',validateTrigger:'blur'}]">
          <text class="jqxAmount"/>
          <uni-easyinput v-if="boole" v-model="ruleForm.syxPolicyCode" @blur="handlePolicyCode(ruleForm.syxPolicyCode)"
                         placeholder="请输入商业险保单号"/>
          <text v-else> {{ ruleForm.syxPolicyCode || '-' }}</text>
        </uni-forms-item>

      </uni-forms>
    </view>
  </view>
</template>

<script>
export default {
  name: 'syxplInfo',//商业险信息
  dicts: ["insurance_channels"],
  props: {
    value: {
      type: Object,
      default: {},
    },
    boole: {
      type: Boolean,
      default: true,
    },
    options: {
      type: Array,
      default: [],
    },
    tableList: {
      type: Array,
      default: [],
    },
    index: {
      type: Number,
      default: -1,
    }
  },
  computed: {
    ruleForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    handleLabel() {
      return function (arr, type) {
        let name = "";
        arr.map((v) => {
          if (type == v.value) {
            name = v.label;
          }
        });
        return name;
      };
    },
  },
  watch: {},
  data() {
    return {
      range: [
        {value: "1", text: "运营"},
        {value: "2", text: "非运营"},
      ],
    };
  },
  created() {
  },
  mounted() {
  },
  methods: {
    handleEffectDate(val) {
      let date = new Date(val)
      return date.setDate(date.getDate() + 1);
    },

    handleSyx(val) {
      if (val) {
        this.value.jqxCompanyId = this.ruleForm.jqxCompanyId = val.supplierId
        this.value.syxCompanyId = this.ruleForm.syxCompanyId = val.supplierId
        this.value.jqxCompany = this.ruleForm.jqxCompany = val.supplierName
        this.value.syxCompany = this.ruleForm.syxCompany = val.supplierName
        this.value.jqxCompanyCode = this.ruleForm.jqxCompanyCode = val.supplierCode
        this.value.syxCompanyCode = this.ruleForm.syxCompanyCode = val.supplierCode
      }
    },
    //性质
    handleInsuranceType(val) {
      if (val) {
        this.value.jqxInsuranceType = this.ruleForm.jqxInsuranceType = val
        this.value.syxInsuranceType = this.ruleForm.syxInsuranceType = val
      }
    },

    //起保日
    handleTime(val) {
      let timeDate = this.addOneMonth(val, 12)
      this.value.syxExpectBeginTime = this.ruleForm.syxExpectBeginTime = val
      this.value.syxExpectEndTime = this.ruleForm.syxExpectEndTime = timeDate
    },

    addOneMonth(dateString, month) {
      const date = new Date(dateString);
      date.setMonth(date.getMonth() + Number(month));
      date.setDate(date.getDate() - 1)
      return date.toISOString().split('T')[0]; // 返回YYYY-MM-DD格式的日期字符串
    },

    blurAmount(str) {
      if (this.ruleForm[str]) {
        let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
        if (!msg.test(this.ruleForm[str])) {
          this.ruleForm[str] = "";
        } else {
          this.ruleForm[str] = this.ruleForm[str]
              .toString()
              .match(/^\d+(?:\.\d{0,2})?/)[0];
        }
      }
    },

    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate().then((res) => {
          resolve();
        }).catch((err) => {
          let key = ".flex-col" + ">>>" + "." + err[0].key;
          let obj = {
            e: key,
            i: this.index
          }
          reject(obj);
        });
      });
    },

    handlePolicyCode(val) {
      if (val) {
        const hasPolicyCode = this.tableList.some(item => [item.jqxPolicyCode, item.syxPolicyCode].includes(val));
        const booleCode = val === this.value.jqxPolicyCode
        this.value.syxPolicyCode = this.ruleForm.syxPolicyCode = hasPolicyCode || booleCode ? '' : val;

      }
    },
  },
};
</script>

<style scoped lang=""></style>


